#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
http server
"""
import os
import tornado.options
import tornado.ioloop
import tornado.httpserver
from tornado.options import define, options
import config.cnf_ini
import app

if __name__ == "__main__":
    cfg_path = os.path.join(os.path.abspath(os.path.curdir), "config", "app.ini")
    cfg = config.cnf_ini.CnfIni(cfg_path)
    define("port", default=cfg.get_attr("app", "port"), help='web server listen port', type=int)
    define("cpus", default=cfg.get_attr("app", "cpus"), help="web server procs", type=int)
    tornado.options.parse_command_line()
    web_app = app.WebApplcation()
    http_server = tornado.httpserver.HTTPServer(web_app)
    if int(options.cpus) == 1:
        http_server.listen(int(options.port))
        http_server.start()
    else:
        http_server.bind(int(options.port))
        http_server.start(int(options.cpus))
    # http_server.listen(int(options.port))
    print 'tornado web server started, listen on: %s, pid: %s' % (options.port, os.getpid())
    # tornado.ioloop.IOLoop.instance().start()
    tornado.ioloop.IOLoop.current().start()
